/**************************************************************************************************
 * light library                                                                                  *
 * Copyright © 2012 David Kretzmer                                                                *
 *                                                                                                *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software  *
 * and associated documentation files (the "Software"), to deal in the Software without           *
 * restriction,including without limitation the rights to use, copy, modify, merge, publish,      *
 * distribute,sublicense, and/or sell copies of the Software, and to permit persons to whom the   *
 * Software is furnished to do so, subject to the following conditions:                           *
 *                                                                                                *
 * The above copyright notice and this permission notice shall be included in all copies or       *
 * substantial portions of the Software.                                                          *
 *                                                                                                *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING  *
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND     *
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.        *
 *                                                                                                *
 *************************************************************************************************/

#pragma once

#include "light/light.hpp"

#include <iostream>
#include <stdexcept>
#include <algorithm>


namespace light
{
namespace io
{
	//=============================================================================================
	//
	//=============================================================================================
	template<typename CharType, typename TraitsType = ::std::char_traits<CharType> >
	class basic_rangebuf : public ::std::basic_streambuf<CharType, TraitsType>
	{
	public:
		typedef ::std::basic_streambuf<CharType, TraitsType> base_streambuf_type;

		typedef typename base_streambuf_type::char_type char_type;
		typedef typename base_streambuf_type::int_type int_type;
		typedef typename base_streambuf_type::pos_type pos_type;
		typedef typename base_streambuf_type::off_type off_type;
		typedef typename base_streambuf_type::traits_type traits_type;


	public:
		//---------------------------------------------------------------------
		// Constructor
		//---------------------------------------------------------------------
		basic_rangebuf(char_type *begin, char_type *end) : base_streambuf_type()
		{
			base_streambuf_type::setg(begin, begin, end);
			base_streambuf_type::setp(begin, end);
		}


		//---------------------------------------------------------------------
		// Move constructor
		//---------------------------------------------------------------------
		basic_rangebuf(basic_rangebuf &&rhs) :
			base_streambuf_type(::std::move(rhs))
		{

		}

		//---------------------------------------------------------------------
		// Move assignment operator
		//---------------------------------------------------------------------
		basic_rangebuf& operator = (basic_rangebuf &&rhs)
		{
			*this = base_streambuf_type::operator=(::std::move(rhs));
			return *this;
		}
	};																		 


	//=============================================================================================
	//
	//=============================================================================================
	template<typename CharType, typename TraitsType = ::std::char_traits<CharType> >
	class basic_irangestream : public ::std::basic_istream<CharType, TraitsType>
	{
	public:
		basic_irangestream(CharType const *begin, CharType const *end) :
			::std::basic_istream<CharType, TraitsType>(&m_buffer),
			m_buffer(const_cast<CharType*>(begin), const_cast<CharType*>(end))
		{

		}

	private:
		basic_rangebuf<CharType, TraitsType> m_buffer;
	};

	template<typename CharType, typename TraitsType = ::std::char_traits<CharType> >
	class basic_orangestream : public ::std::basic_ostream<CharType, TraitsType>
	{
	public:
		basic_orangestream(CharType *begin, CharType *end) :
			::std::basic_ostream<CharType, TraitsType>(&m_buffer),
			m_buffer(begin, end)
		{

		}

	private:
		basic_rangebuf<CharType, TraitsType> m_buffer;
	};

	template<typename CharType, typename TraitsType = ::std::char_traits<CharType> >
	class basic_iorangestream : public ::std::basic_iostream<CharType, TraitsType>
	{
	public:
		basic_iorangestream(CharType *begin, CharType *end) :
			::std::basic_iostream<CharType, TraitsType>(&m_buffer),
			m_buffer(begin, end)
		{

		}

	private:
		basic_rangebuf<CharType, TraitsType> m_buffer;
	};


	typedef basic_rangebuf<char> rangebuf;
	typedef basic_irangestream<char> irangestream;
	typedef basic_orangestream<char> orangestream;
	typedef basic_iorangestream<char> iorangestream;

	typedef basic_rangebuf<wchar_t> wrangebuf;
	typedef basic_irangestream<wchar_t> wirangestream;
	typedef basic_orangestream<wchar_t> worangestream;
	typedef basic_iorangestream<wchar_t> wiorangestream;

} // namespace: io

using io::basic_rangebuf;
using io::rangebuf;
using io::wrangebuf;

using io::basic_irangestream;
using io::basic_orangestream;
using io::basic_iorangestream;
using io::irangestream;
using io::orangestream;
using io::iorangestream;
using io::wirangestream;
using io::worangestream;
using io::wiorangestream;

} // namespace: light

